package com.xuxin.pda.model;

import com.xuxin.pda.bean.TrashEnterBean;
import com.xuxin.pda.contract.TrashEnterContract;
import com.xuxin.pda.network.Retrofit;
import com.xuxin.pda.network.RetrofitApi;
import com.xuxin.pda.presenter.TrashEnterPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2021/2/7 0007
 */
public class TrashEnterModel implements TrashEnterContract.Model {
    @Override
    public void trashEnter(TrashEnterPresenter presenter, String WarehouseId, String InWarehouseTime, String UserId, String BoxIds) {
        try {
            JSONObject object = new JSONObject();
            object.put("WarehouseId", WarehouseId);
            object.put("InWarehouseTime", InWarehouseTime);
            object.put("UserId", UserId);
            object.put("BoxIds", BoxIds);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .trashEnter(WarehouseId,InWarehouseTime,UserId,BoxIds)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new DisposableObserver<TrashEnterBean>() {
                        @Override
                        public void onNext(@NonNull TrashEnterBean trashEnterBean) {
                            if (trashEnterBean.isSuccess()) {
                                presenter.onSuccess(trashEnterBean);
                            } else {
                                presenter.onError(trashEnterBean);
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}